package ano.it.service.impl;

import ano.it.dao.GreenHouseDao;
import ano.it.entity.PageResult;
import ano.it.entity.QueryPageBean;
import ano.it.jobs.DocumentDetailsJobs;
import ano.it.pojo.DetailedData;
import ano.it.pojo.Device;
import ano.it.pojo.GreenHouse;
import ano.it.service.GreenHouseService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.xmlbeans.impl.soap.DetailEntry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service(interfaceClass = GreenHouseService.class)
public class GreenHouseServiceImpl implements GreenHouseService{
    @Autowired
    private GreenHouseDao greenHouseDao;

    @Override
    public void add(GreenHouse greenHouse, Integer[] deviceIds) {
        greenHouseDao.add(greenHouse);

        Integer id = greenHouse.getId();
        System.out.println(id);
        this.setGreenHouseAndDevice(id,deviceIds);
    }

    //大棚信息
    public void adddetail( DetailedData detailedData) {
        greenHouseDao.adddetail(detailedData);
    }
    private void setGreenHouseAndDevice(Integer greenhouseId,Integer[] deviceIds) {
        for (Integer deviceId : deviceIds) {
            Map<String,Integer> map=new HashMap<>();
            map.put("deviceId",deviceId);
            map.put("greenhouseId",greenhouseId);
            greenHouseDao.setGreenHouseAndDevice(map);
        }
    }
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<GreenHouse> page=greenHouseDao.selectByCondition(queryPageBean.getQueryString());
        long total=page.getTotal();
        List<GreenHouse> rows=page.getResult();
//        查询详细的功能
        for (GreenHouse row : rows) {
            GreenHouse greenHouse=greenHouseDao.selectDetail(row.getId());
            if(greenHouse!=null){
                row.setCo(greenHouse.getCo());
                row.setWendu(greenHouse.getWendu());
            }

        }

        return new PageResult(total,rows);
    }

    @Override
    public void delete(Integer id) {
        greenHouseDao.deleteById(id);
    }

    @Override
    public GreenHouse findById(Integer id) {
        return greenHouseDao.findById(id);
    }

    @Override
    public void edit(GreenHouse greenHouse, Integer[] deviceIds) {
        greenHouseDao.update(greenHouse);
//        删除关联关系在进行添加
        greenHouseDao.deletegreenhousedevice(greenHouse.getId());

        Integer id = greenHouse.getId();

        this.setGreenHouseAndDevice(id,deviceIds);
    }

    @Override
    public List<Integer> findDeviceIdsByGreenHouseId(Integer id) {
        return greenHouseDao.findDeviceIdsByGreenHouseId(id);
    }

    @Override
    public GreenHouse getReportData(Integer id) {
        GreenHouse greenHouse=greenHouseDao.selectByIdGreenHouse(id);

        GreenHouse greenHouse1=greenHouseDao.selectDetail(id);

        if(greenHouse1!=null){
            greenHouse.setCo(greenHouse1.getCo());
            greenHouse.setWendu(greenHouse1.getWendu());
        }
        return greenHouse;
    }

}
